﻿using System; 

namespace Plugin
{
    public class Model : MarshalByRefObject
    {
		public int Version = 4;

        public bool[] Hidden = new bool[0];
        public string[] ElementName = new string[0];
        public string[] Text = new string[0];
        public int[] Left = new int[0];
        public int[] Top = new int[0];
        public int[] FontSize = new int[0];
        public bool[] AlignRight = new bool[0];
        public int[] Beeps = new int[0]; 
        public string[] MacroNames = new string[0]; 
        public string[] MacroValues = new string[0];  

        public void Execute(ref float[] Predictions, float[] PredictionCorrectionSlopes, float[] PredictionCorrectionIntercepts, string MeasurementNumberAndCount,
                            String ModelName, float[] ModelFits,
                            String GPSLocation, String FileName, DateTime CreatedDateTime,
                            String SampleNumberAndLotNumber, int SampleNumber, int LotNumber, float Prediction1LotAverage, float Prediction1LotStandardDeviation,
                            int FirstWavelength, float[] AbsorbanceSpectra, float[] FirstDerivativeSpectra, float[] SecondDerivativeSpectra, float[] MacroSpectra)
        {

%ModelCode%
        }      
		
		
        public string Format(string aValue, int aLength)
        {
			if (aValue.Length > aLength)
			{
				return aValue.Substring(0, aLength);
			}
			else 
			{			
				return aValue;
			}
		}

		
        public string Format(double aValue, int aDecimalPlaces)
        {
            if ((aValue > int.MaxValue) || (aValue < int.MinValue))
            {
                return "0." + new String('0', aDecimalPlaces);
            }

            if (aValue == 0)
            {
                return "0.0";
            }

            aValue = System.Math.Round((float)aValue * 1000000) / 1000000;

            int intValueForFirstHalf = (int)aValue;
            string firstHalf = intValueForFirstHalf.ToString();

            int intValueForSecondHalf = (int)System.Math.Round((System.Math.Abs(aValue) - System.Math.Abs(intValueForFirstHalf)) * (System.Math.Pow(10, aDecimalPlaces)));// ((System.Math.Abs(aValue) - System.Math.Abs(intValueForFirstHalf)) * (System.Math.Pow(10, aDecimalPlaces)));
            string secondHalf = intValueForSecondHalf.ToString();
            secondHalf = (new String('0', aDecimalPlaces) + secondHalf).Substring(secondHalf.Length, aDecimalPlaces);
            
            return firstHalf + "." + secondHalf;
        }  
    }
}